package au.org.ala.phylojive

class Tree {
    User owner
    String name
    String privateUrlPath
    Boolean isPublic = true
    String newick
    String characters
    Template presentClade
    String identifyLifeDataset
    String attribution
    Integer highlight

    static constraints = {
        name (unique: true, blank: false)
        owner (blank: false)
        privateUrlPath (nullable: true, blank: true)
        isPublic (nullable: true, blank: true)
        newick (blank: false, widget: 'textarea')
        characters (nullable: true, widget: 'textarea')
        presentClade (nullable: true, blank: true)
        attribution (nullable: true, widget: 'textarea')
        identifyLifeDataset (nullable: true)
        highlight (nullable: true, range: 1..10)
    }

    static mapping = {
        newick type: 'text'
        attribution type: 'text'
        presentClade type: 'text'
        characters sqlType: 'clob'
    }

    def beforeValidate() {
        // automcatically create a privateUrl hashcode
        if (!privateUrlPath) {
            def input = name + new Date().toString()
            privateUrlPath = input.bytes.encodeBase64().toString()
        }

        if (!identifyLifeDataset) {
//            identifyLifeDataset = "."
        }

        if (!attribution) {
//            attribution = "."
        }
    }
}

